package com.wangtao.firstapp.fragments.communityfragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.ArticleDetails;
import com.wangtao.firstapp.activity.MyFollowsDetailsActivity;
import com.wangtao.firstapp.adapters.MyFollowsArticlesAdapter;
import com.wangtao.firstapp.inter.OnRVItemClickListener;
import com.wangtao.firstapp.inter.OnRVItemLongClickListener;
import com.wangtao.firstapp.modles.FollowsArticleData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 文章列表下关注动态文章Fragment页面
 * Created by lanouhn on 16/8/31.
 */
public class MyFollowsArticlesFragment extends Fragment implements OnRVItemClickListener, OnRVItemLongClickListener {
    private FollowsArticleData followsArticleData;
    private RecyclerView recyclerView;
    private MyFollowsArticlesAdapter followsArticlesAdapter;
    //private SwipeRefreshLayout swiperefreshlayout_myfollows;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.followsnotempty, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_followsarticle);
        //swiperefreshlayout_myfollows = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout_myfollows);
        getData();
        // SwipeToRefresh();
        return view;
    }

    private void getData() {
        String url = "http://open4.bantangapp.com/topics/article/listByMyAttention";
        String param = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=meizu&app_installtime=1471923362&app_versions=5.8.7&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=10";

        try {
            HttpUtils.doPostAsyn(url, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    followsArticleData = gson.fromJson(result, FollowsArticleData.class);
                    if (followsArticleData.getData().getTopic().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    //  Toast.makeText(getActivity(), "网络访问失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    //LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()) {
                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    };
                    followsArticlesAdapter = new MyFollowsArticlesAdapter(getActivity(), followsArticleData);
                    recyclerView.setAdapter(followsArticlesAdapter);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    followsArticlesAdapter.setClickListener(MyFollowsArticlesFragment.this);
                    followsArticlesAdapter.setLongClickListener(MyFollowsArticlesFragment.this);
                    break;
            }
        }
    };

    @Override
    public void OnRVItemClickListener(int position) {
        // 图文详情的网址
        String address = "http://open4.bantangapp.com/topic/newInfo";
        String parameter = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + followsArticleData.getData().getTopic().get(position).getId() + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";
        // 大家都在看 的网址
        String address1 = "http://open4.bantangapp.com/topics/topic/relationRec";
        String parameter1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + followsArticleData.getData().getTopic().get(position).getId();
        // 评论  网址
        String address2 = "http://open4.bantangapp.com/topics/comment/list";
        String parameter2 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=1&pagesize=20&id=" + followsArticleData.getData().getTopic().get(position).getId();
        Intent intent_click = new Intent(getActivity(), MyFollowsDetailsActivity.class);
        intent_click.putExtra("url", address);
        intent_click.putExtra("url1", address1);
        intent_click.putExtra("url2", address2);
        intent_click.putExtra("param", parameter);
        intent_click.putExtra("param1", parameter1);
        intent_click.putExtra("param2", parameter2);
        startActivity(intent_click);
    }

    @Override
    public void OnRVItemLongClickListener(int position) {
        // 图文详情的网址
        String address = "http://open4.bantangapp.com/topic/newInfo";
        String parameter = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + followsArticleData.getData().getTopic().get(position).getId() + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";
        // 大家都在看 的网址
        String address1 = "http://open4.bantangapp.com/topics/topic/relationRec";
        String parameter1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + followsArticleData.getData().getTopic().get(position).getId();
        // 评论  网址
        String address2 = "http://open4.bantangapp.com/topics/comment/list";
        String parameter2 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=0&pagesize=20&id=" + followsArticleData.getData().getTopic().get(position).getId();
        Intent intent_click = new Intent(getActivity(), MyFollowsDetailsActivity.class);
        intent_click.putExtra("url", address);
        intent_click.putExtra("url1", address1);
        intent_click.putExtra("url2", address2);
        intent_click.putExtra("param", parameter);
        intent_click.putExtra("param1", parameter1);
        intent_click.putExtra("param2", parameter2);
        startActivity(intent_click);
    }

    // 下拉刷新
//    private void SwipeToRefresh() {
//        swiperefreshlayout_myfollows.setColorSchemeColors(Color.CYAN, Color.BLUE, Color.GREEN, Color.YELLOW);
//        swiperefreshlayout_myfollows.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                swiperefreshlayout_myfollows.setRefreshing(true);
//                (new Handler()).postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        swiperefreshlayout_myfollows.setRefreshing(false);
//                        followsArticlesAdapter.notifyDataSetChanged();
//                    }
//                }, 3000);
//            }
//        });
//    }
}
